<?php

namespace app\admin\controller\datacenter;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Clientorder extends Backend
{
    
    /**
     * YOnlineorder模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('VDayOrder');

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //按时间搜索
        $startime = $this->request->param('startime');
        $endtime = $this->request->param('endtime');
        if(empty($startime) && empty($endtime)){
            $startime = strtotime('-30 days');
            $endtime = time();
        }elseif(empty($startime) && !empty($endtime)){
            $endtime = strtotime($endtime);
            $startime = $endtime-(86400*30);
        }elseif(!empty($startime) && empty($endtime)){
            $startime = strtotime($startime);
            $endtime = $startime+(86400*30);
        }else{
            $startime = strtotime($startime);
            $endtime = strtotime($endtime);
            if(($endtime - $startime) >(86400*30)){
               $this->error('最多查30天记录'); 
            }
        }
        
        $map['addtime'] = array('between', [$startime,$endtime]);
        $map['channel'] = 2;
        
        //搜索30条
        $paynum = $amount = [];
        $list = Db::name('v_day_order')->where($map)->order('addtime asc')->select();
        foreach ($list as $key=>$val){
            $paynum[date('m.d',$val['addtime'])] = $val['paynum'];//订单数量
            $amount[date('m.d',$val['addtime'])] = $val['amount'];//订单金额
        }
    
        $this->view->assign([
            'paynum'          => $paynum,
            'amount'          => $amount,
            'is_mobile' => is_mobile()?:0,
            'endtime' => date('Y-m-d',$endtime),
            'startime' => date('Y-m-d',$startime),
        ]);
        
        return $this->view->fetch();
    }
    
    /**
     * 每天订单统计列表
     */
    public function orderList(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('pkey_name'))
        {
            return $this->selectpage();
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        
        $total = $this->model
        ->where($where)
        ->where(['channel'=>2])
        ->order($sort, $order)
        ->count();
    
        $list = $this->model
        ->where($where)
        ->where(['channel'=>2])
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->select();

        //统计
        $timestamp = time();
        $w_star = strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp)));
        $w_end = strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))) + 24 * 3600 - 1;
        $statistics = [];
        $count_num = $this->model->field('sum(paynum) as paynum,sum(amount) as amount')->where(['channel'=>2])->find();//总
        $m_num = $this->model->field('sum(paynum) as paynum,sum(amount) as amount')->where(['channel'=>2])->where("FROM_UNIXTIME(addtime,'%Y-%m') = date_format(now(),'%Y-%m')")->find();//月
        $w_num = $this->model->field('sum(paynum) as paynum,sum(amount) as amount')->where(['channel'=>2,'addtime'=>array('between', [$w_star,$w_end])])->find();//周
        $d_num = Db::name('y_onlineorder')->field('count(*) as paynum,sum(orderamount) as amount')->where(['channel'=>2,'status'=>2])->where("FROM_UNIXTIME(addtime/1000,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')")->find();//今天充值金额
        $statistics['c_paynum'] = empty($count_num['paynum'])?0:$count_num['paynum'];
        $statistics['c_amount'] = empty($count_num['amount'])?0:$count_num['amount'];
        $statistics['m_paynum'] = empty($m_num['paynum'])?0:$m_num['paynum'];
        $statistics['m_amount'] = empty($m_num['amount'])?0:$m_num['amount'];
        $statistics['w_paynum'] = empty($w_num['paynum'])?0:$w_num['paynum'];
        $statistics['w_amount'] = empty($w_num['amount'])?0:$w_num['amount'];
        $statistics['d_paynum'] = empty($d_num['paynum'])?0:$d_num['paynum'];
        $statistics['d_amount'] = empty($d_num['amount'])?0:$d_num['amount'];
        
        $result = array("total" => $total, "rows" => $list, "statistics"=>$statistics);
    
        return json($result);
    }

}
